package com.caowei.weicimpress.weicimpress;

import android.content.Context;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.LinearLayout;

import com.caowei.weicimpress.weicimpress.widget.ActionItem;
import com.caowei.weicimpress.weicimpress.widget.DropdownListWidget;


/**
 * Created by caoweiPc on 2014/7/18.
 * todo   没完
 */
public class MoreMenuActionView extends LinearLayout implements View.OnClickListener {
    private Context mContext;
    private DropdownListWidget menuListWidget;
    private int mMaxWidth;
    /**
     * Creates a new instance. ActionProvider classes should always implement a
     * constructor that takes a single Context parameter for inflating from menu XML.
     *
     * @param context Context for accessing resources.
     */
    public MoreMenuActionView(Context context) {
        this(context,null);
    }

    public MoreMenuActionView(Context context,AttributeSet attributes){
        super(context, attributes);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.menumore, this, true);

        View menu=findViewById(R.id.menu);
        menu.setOnClickListener(this);
    }

    private int getPreferredWidth() {
        return getContext().getResources()
                .getDimensionPixelSize(R.dimen.actionbaritem_w);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        switch (widthMode) {
            case MeasureSpec.AT_MOST:
                // If there is an upper limit, don't exceed maximum width (explicit or implicit)
                if (mMaxWidth > 0) {
                    width = Math.min(mMaxWidth, width);
                } else {
                    width = Math.min(getPreferredWidth(), width);
                }
                break;
            case MeasureSpec.EXACTLY:
                // If an exact width is specified, still don't exceed any specified maximum width
                if (mMaxWidth > 0) {
                    width = Math.min(mMaxWidth, width);
                }
                break;
            case MeasureSpec.UNSPECIFIED:
                // Use maximum width, if specified, else preferred width
                width = mMaxWidth > 0 ? mMaxWidth : getPreferredWidth();
                break;
        }
        widthMode = MeasureSpec.EXACTLY;
        super.onMeasure(MeasureSpec.makeMeasureSpec(width, widthMode), heightMeasureSpec);

    }

    //todo
    public View onCreateActionView() {
        View v= LayoutInflater.from(getContext()).inflate(R.layout.menumore,null);
        LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        );
        layoutParams.leftMargin=50;
        layoutParams.rightMargin=50;
        layoutParams.gravity= Gravity.CENTER;
        v.setLayoutParams(layoutParams);
        v.setOnClickListener(this);
        return v;
    }

    @Override
    public void onClick(View v) {
        if(menuListWidget==null){
            //todo test
            menuListWidget=new DropdownListWidget(getContext(),new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //todo
                }
            });
            ActionItem actionItem=new ActionItem();

           /* actionItem.setMenuName("任务");
            actionItem.setIcoId(R.drawable.setting);
            actionItem.setCallback(new ActionItem.Callback() {
                @Override
                public void callback(Object... params) {
                    //todo  go 任务 activity
                }
            });
            menuListWidget.addActionItem(actionItem);*/
            actionItem=new ActionItem();
            actionItem.setMenuName("设置");
            actionItem.setIcoId(R.drawable.setting);
            actionItem.setCallback(new ActionItem.Callback() {
                @Override
                public void callback(Object... params) {
                    //todo  go setting activity
                }
            });
            menuListWidget.addActionItem(actionItem);
            actionItem=new ActionItem();
            actionItem.setMenuName("退出");
            actionItem.setCallback(new ActionItem.Callback() {
                @Override
                public void callback(Object... params) {
                    Utils.exit();
                }
            });
            actionItem.setIcoId(R.drawable.exit);
            menuListWidget.addActionItem(actionItem);
            menuListWidget.setWidth(182);
        }
        if(!menuListWidget.isShowing()){
            ActionBarActivity actionBarActivity= ActivityManager.getInstance().peekTop();
            ActionBar actionBar=actionBarActivity.getSupportActionBar();
            int actionBarH= actionBar.getHeight();
            View decorV= actionBarActivity.getWindow().getDecorView();
            View contentV= decorV.findViewById(Window.ID_ANDROID_CONTENT);
            int rh= decorV.getHeight()-contentV.getHeight();
            View vp= (View) v.getParent().getParent();
            menuListWidget.show(v,vp.getHeight()+(rh-actionBarH));
        }else{
            menuListWidget.dismiss();
        }
    }
}
